//
//  RTRemoteVideoView.m
//  StoryToy
//
//  Created by baxiang on 2018/3/20.
//  Copyright © 2018年 roobo. All rights reserved.
//

#import "RTRemoteVideoView.h"



@implementation RTRemoteVideoView{
    BOOL isInitDone;
}
@synthesize videoSize = _videoSize;

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        _videoSize = CGSizeMake(640, 480);
        
        //_viewModle = RBVideoAspectFill;
    }
    
    isInitDone = YES;
    
    return self;
}


- (instancetype)init{
    if(self = [super initWithFrame:CGRectMake(0, 0, 1280, 720)]){
        
        _videoSize = CGSizeMake(1280, 720);
        
        isInitDone = YES;
    }
    
    return self;
    
}

- (void)setVideoSize:(CGSize)videoSize{
    _videoSize = videoSize;
    self.frame = self.frame;
}



- (void)setFrame:(CGRect)frame{
    _nframe = frame;
     CGRect fr = frame;;
    if(isInitDone){
       
        
//        switch (_viewModle) {
//            case RBVideoFill:
//                fr
//                break;
//            case RBVideoAspectFit:
//                fr = [self getAspectFrame:frame];
//                break;
//            case RBVideoAspectFill:
//                fr = [self getAspectFullFrame:frame];
//                break;
//            default:
//                break;
//        }
//
        float scale = 0 ;
        if(fr.size.width > 0 && fr.size.height > 0){
            scale = MAX(frame.size.width/_videoSize.width, frame.size.height/_videoSize.height);
        }else{
            scale = [UIApplication sharedApplication].keyWindow.bounds.size.width/_videoSize.width;
        }
        
        self.layer.frame = fr;
    }else{
        [super setFrame:frame];
        
    }
    
    
}
- (CGRect)getAspectFullFrame:(CGRect )rect{
    if(self.videoSize.width == 0 || self.videoSize.height == 0 || CGRectGetWidth(_nframe) == 0 || CGRectGetHeight(_nframe)== 0)
        return rect;
    float sc = self.videoSize.width/self.videoSize.height;
    float sv = _nframe.size.width/_nframe.size.height;
    float width = 0;
    float height = 0;
    if(sc > sv){
        height = _nframe.size.height;
        width = height*sc;
        
    }else{
        width = _nframe.size.width;
        height = width/sc;
        
    }
    return CGRectMake(CGRectGetMidX(_nframe) - width/2, CGRectGetMidY(_nframe) - height/2, width, height);
    
}


- (CGRect)getAspectFrame:(CGRect )rect{
    if(self.videoSize.width == 0 || self.videoSize.height == 0 || CGRectGetWidth(_nframe) == 0 || CGRectGetHeight(_nframe) == 0)
        return rect;
    float sc = self.videoSize.width/self.videoSize.height;
    float sv = _nframe.size.width/_nframe.size.height;
    float width = 0;
    float height = 0;
    if(sc > sv){
        width = _nframe.size.width;
        height = width/sc;
    }else{
        height = _nframe.size.height;
        width = height*sc;
        
    }
    return CGRectMake(CGRectGetMidX(_nframe) - width/2, CGRectGetMidY(_nframe) - height/2, width, height);
    
}



@end

